<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\admin\controller;
use \think\Controller;
use app\admin\Model\Admin as adminModel;
use \think\captcha\src\Captcha;
class Login extends Controller{
    public function index(){
        if(request()->isPost()){
            $data['username']=input('username');
            $data['password']=input('password');
            $admin=new adminModel();
            $res=$admin->loginAdmin($data);
            $this->check(input('code'));
            if( $res == 1){
                return $this->error('帐号不存在');
            }else if($res== 2){
                return $this->success("登录成功",url('index/index'));
            }else if($res==3){
                return $this->error('密码错误');
            }else{
            }
        }
        return view('login');
    }
    
    public function logout(){
        session(null);
        $this->success('退出系统成功','/login');

    }

    public function captcha_img() {
        $captcha = new Captcha();
        //验证码过期时间（s）
        $captcha->expire = 1800;
        //是否画混淆曲线
        $captcha->useCurve = true;
        //是否添加杂点
        $captcha->useNoise = true;
        //验证码位数
        $captcha->length = 4;
        //验证成功后是否重置
        $captcha->reset = true;
        $captcha->imageH =50;
        $captcha->fontSize =21;
        return $captcha->entry();
    }
    public function check($code=""){
        $captcha=new Captcha();
        if(!$captcha->check($code)){
            $this->error('验证码错误');
        }else{
            return true;
        }
    }
}